﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model;

namespace Backend.JYD.Business
{
    public class RoleManager
    {
        static readonly IDAL.IRoleInfo idal = Factory.DALFactory.CreateEFDAL<IDAL.IRoleInfo>("DAL.RoleInfoDAL");
        static readonly IDAL.IEmpRoleInfo idalEmp = Factory.DALFactory.CreateEFDAL<IDAL.IEmpRoleInfo>("DAL.EmpRoleInfoDAL");

        #region List<EmpRoleInfo>
        
        static List<EmpRoleInfo> _allEmpRoleInfo = null;
        static object _lockPadEmpRoleInfo = new object();
        List<EmpRoleInfo> AllEmpRoleInfo
        {
            get
            {
                if (_allEmpRoleInfo == null)
                {
                    lock (_lockPadRoleInfo)
                    {
                        if (_allEmpRoleInfo == null)
                        {
                            var query = idalEmp.GetAllEmpRoleInfo();
                            if (query != null)
                            {
                                _allEmpRoleInfo = query.ToList();
                            }
                        }
                    }
                }

                return _allEmpRoleInfo;
            }
        }

        #endregion

        #region List<RoleInfo>

        static List<RoleInfo> _allRoleInfo = null;
        static object _lockPadRoleInfo = new object();
        List<RoleInfo> AllRoleInfo
        {
            get
            {
                if (_allRoleInfo == null)
                {
                    lock (_lockPadRoleInfo)
                    {
                        if (_allRoleInfo == null)
                        {
                            var query = idal.GetAllRoleInfo();
                            if (query != null)
                            {
                                _allRoleInfo = query.ToList();
                            }
                        }
                    }
                }
                return _allRoleInfo;
            }
        }

        #endregion

        public RoleManager() { }

        public void InitEmpRoleInfo()
        {
            lock (_lockPadEmpRoleInfo)
            {
                new AuthManager().ClearEmpAuthorityInfoes();
                var query = idalEmp.GetAllEmpRoleInfo();
                if (query != null)
                {
                    _allEmpRoleInfo = query.ToList();
                }
            }
        }

        public void InitRoleInfo()
        {
            lock (_lockPadRoleInfo)
            {
                new AuthManager().ClearEmpAuthorityInfoes();
                var query = idal.GetAllRoleInfo();
                if (query != null)
                {
                    _allRoleInfo = query.ToList();
                }
            }
        }

        public RoleInfo GetMainRoleInfoByEmpId(Int64 empId)
        {
            if (this.AllRoleInfo == null) return null;

            var empRoleInfo = this.GetMainEmpRoleInfoByEmpId(empId);
            if (empRoleInfo == null) return null;

            var roleInfo = this.AllRoleInfo.Find(p => empRoleInfo.RoleId == p.RoleId);
            return roleInfo;
        }

        public List<RoleInfo> GetRoleInfoByEmpId(Int64 empId)
        {
            if (this.AllRoleInfo == null) return null;

            var empRoleInfos = this.GetEmpRoleInfoByEmpId(empId);
            if (empRoleInfos == null) return null;

            var roleInfos = this.AllRoleInfo.FindAll(p => empRoleInfos.ToList().Exists(p1 => p1.RoleId == p.RoleId));
            return roleInfos;
        }

        public EmpRoleInfo GetMainEmpRoleInfoByEmpId(Int64 empId)
        {
            if (this.AllEmpRoleInfo == null) return null;

            return this.AllEmpRoleInfo.Find(p => p.EmpId == empId && p.Type == 2);
        }

        public List<EmpRoleInfo> GetEmpRoleInfoByEmpId(Int64 empId)
        {
            if (this.AllEmpRoleInfo == null) return null;

            return this.AllEmpRoleInfo.FindAll(p => p.EmpId == empId);
        }

        public List<RoleInfo> GetRoleInfoByRoleIds(List<Int64> roleIds)
        {
            if (roleIds == null || roleIds.Count <= 0) return null;
            if (this.AllRoleInfo != null)
            {
                return this.AllRoleInfo.FindAll(p => roleIds.Contains(p.RoleId));
            }
            return null;
        }
    }
}
